package com.sptpc.learnspring.controller;

import com.sptpc.learnspring.controller.request.ClearOrderRequest;
import com.sptpc.learnspring.controller.request.NewOrderRequest;
import com.sptpc.learnspring.model.Drink;
import com.sptpc.learnspring.model.Order;
import com.sptpc.learnspring.service.DrinkService;
import com.sptpc.learnspring.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private DrinkService drinkService;

    @GetMapping("/cart")
    public ModelAndView addCart(HttpSession session, @RequestParam Long drink) {
        ModelAndView mv = new ModelAndView("order/cart");

        List<Drink> cart = (List<Drink>) session.getAttribute("cart");
        if (cart == null) {
            cart = new ArrayList<>();
        }
        cart.add(drinkService.getDrinkById(drink));
        session.setAttribute("cart", cart);

        return mv;
    }

    @GetMapping(value = "/myorder")
    public ModelAndView getOrderByName(HttpSession session, @RequestParam String customer){
        ModelAndView mv = new ModelAndView("order/myorder");
        List<Order> orders = orderService.getOrders(customer);
        mv.addObject("orders",orders);
        session.setAttribute("customer",customer);

        return mv;
    }

    @PostMapping(value = "/myorder/search")
    public ModelAndView getOrderByTime(HttpSession session,
                                       @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                       @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime){
        ModelAndView mv = new ModelAndView("order/myorder");
        LocalDateTime s = startTime.atTime(0,0);
        LocalDateTime e = endTime.atTime(0,0);
        String customer = (String) session.getAttribute("customer");
        List<Order> orders = orderService.getOrdersByTime(s, e, customer);
        mv.addObject("orders",orders);

        return mv;
    }

    @PostMapping(value = "/save")
    public ModelAndView saveOrder(HttpSession session,NewOrderRequest orderRequest){
        // session购物车
        session.removeAttribute("cart");

        Order order = orderService.saveOrder(orderRequest);
        ModelAndView mv = new ModelAndView("order/complete");
        mv.addObject("order",order);

        return mv;
    }

//    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_JSON_VALUE)
//    @ResponseBody
//    public Order createNewOrderWithJson(@RequestBody NewOrderRequest orderRequest){
//
//        return orderService.saveOrder(orderRequest);
//    }
//
    @GetMapping(value = "/clear")
    public ModelAndView clearOrder(HttpSession session,ClearOrderRequest clearOrderRequest){
        ModelAndView mv = new ModelAndView("order/myorder");
        orderService.clearOrder(clearOrderRequest);
        String customer = (String) session.getAttribute("customer");
        List<Order> orders = orderService.getOrders(customer);
        mv.addObject("orders",orders);

        return mv;
    }

//    @GetMapping(value = "/{customer}")
//    public List<Order> getOrderByName(@PathVariable String customer){
//
//        return orderService.getOrders(customer);
//    }
//
//    @GetMapping("/delete")
//    public void deleteOrder(@RequestParam Long orderId) {
//
//        orderService.delete(orderId);
//    }

}
